;*****************************************************
; 	Copyright (C) SEIKO EPSON CORP. 1997-2001
;	All rights Reserved
;
;	File name : strncat.s
;	Function  :
;	       This is strncat function
;	Revision  :
;		1997/03/24	V.Chan start
;		2001/12/06	T.Tazaki for GNU33(Linux)
;*****************************************************

;*****************************************************
; strncat
;	string append from src to end of dest (N bytes)
;	does not append characters after NULL
;	0 terminating character is added
;
; arguments : %r6:dest addr, %r7:src addr, %r8:byte number
; return    : %r4:dest addr
;*****************************************************

	.section .text
	.align 1
	.global strncat
	.type	strncat,@function

strncat:
	ld.w 	%r4, %r6		; return dest add
	
pos_loop:
	ld.ub	%r10, [%r6]+	; position to end of string1
	cmp		%r10, 0
	jrne	pos_loop

	sub		%r6, 1			; position on NULL byte

strncat_loop:
	cmp		%r8, 0
	jreq	lastnull
	ld.ub	%r10, [%r7]+	; copy src 1 byte to dest
	ld.b	[%r6]+, %r10
	cmp		%r10, 0
	jreq	strncat_ret
	jp.d	strncat_loop
	sub		%r8, 1			; decrement count

lastnull:
	ld.w	%r10, 0
	ld.b	[%r6], %r10		; add last NULL

strncat_ret:
	ret

